package LeetCode刷题;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-10-12 16:22
 * @description: Functions of this class is
 * 官方题解（动态规划）：
 * class Solution {
 *     public int maxProfit(int[] prices) {
 *         int n = prices.length;
 *         int[][] dp = new int[n][2];
 *         dp[0][0] = 0;
 *         dp[0][1] = -prices[0];
 *         for (int i = 1; i < n; ++i) {
 *             dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
 *             dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
 *         }
 *         return dp[n - 1][0];
 *     }
 * }
 *
 **/
public class No买卖股票的最佳时机II {
    public static void main(String args[]){

    }
    class Solution {
        //贪心做法
        public int maxProfit(int[] prices) {
            if(prices.length==1){
                return 0;
            }
            int ans=0;
            for(int i=0;i<prices.length-1;i++){
                if(prices[i]<prices[i+1]){
                    ans+=(prices[i+1]-prices[i]);
                }
            }
            return ans>0?ans:0;
        }
    }
}